<?php

namespace app\common\model;

use Think\Db;
use think\Model;

/**
 * 设备类别
 */
class IconTypes extends Base
{
    //    protected $table = 'my_machines_type';

    public static function status($key = null)
    {
        $s = [1 => '正常', 2 => '废弃', 3 => '冻结'];
        if ($key === null) {
            return $s;
        }
        return $s[$key];
    }


    private static $mode = ['line' => '线', 'point' => '点', 'area' => '区域',];


    public static function getMode()
    {
        return self::$mode;
    }


    public static function getModeKey()
    {
        return array_keys(self::$mode);
    }


    /**
     * 删除类别
     */
    public function remove($id, $fake = false)
    {
        $id     = $id ?: input('id');
        $result = $this->where("id={$id} or pid={$id}")->delete();
        if (false !== $result) {
            return [1, "删除成功:" . input('id')];
        }
        else {
            return [0, $this->getError()];
        }
    }


    /**
     *  获取主类别
     * @param type $parentId
     * @return type
     */
    public function getMainTypes()
    {
        return (Object)$this->where('pid', 0)->order('sort', 'desc')->select()->toArray();
    }


    public function getAllTypes()
    {
        $where = $this->_where();
        $data  = $this->where($where)->order(['sort' => 'desc', 'id' => 'asc'])->select()->toArray();

        return $data = sortTree($data, 'id', 'pid', 0);
    }


    public function getAllTypeName()
    {
        $d = $this->getAllTypes();
        return array_map(function ($arr) {
            return $arr['name'];
        }, $d);
    }


    public function _where()
    {
        $where = ' 1 ';
        if (input('get.key/a')) {
            foreach (input('get.key/a') as $k => $v) {
                if (!empty($v)) {
                    switch ($k) {
                        case "name":
                            $where .= " and {$k} like '%{$v}%'";
                            break;
                        case "time":
                            $applydate = explode("--", $v);
                            $start     = strtotime($applydate[0]);
                            $end       = strtotime($applydate[1]);
                            $where     .= " and '{$start}' <=  unix_timestamp(`time`) and '{$end}' >= unix_timestamp(`time`) ";
                            break;
                    }
                }
            }
        }

        return $where;
    }


    public function simpleTree()
    {
        $data = $this->getAll();
        if (empty($data))
            return false;
        return array2tree($data, 'id', 'pid', 'name', 0);
    }


    /**
     * 所有子分类
     */
    public function getSubTypesById($id)
    {
        return $this->where("instr(`path`,'-{$id}-') or substr(`path`,-length('-{$id}'))='-{$id}'")->select()->toArray();
    }


    public function getNameList($ids)
    {

        if (empty($ids))
            return '';
        $names = self::getNames();
        $tmp   = [];
        foreach (explode(',', $ids) as $v) {
            if (isset($names[$v])) {
                $tmp[] = $names[$v];
            }
        }

        return implode(',', $tmp);
    }

}
